<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>js操作class，且支持链式操作</title>
    <style>
      #app {
        width: 100px;
        height: 100px;
      }
      .test1 {
        background: red;
      }
      .test2 {
        background: green;
      }
    </style>
  </head>
  <body>
    <div id="app" class="test1 dd" style="background: yellow">内容区域</div>
  </body>
</html>
<script>
  class Handler {
    constructor(id) {
      this.el = document.getElementById(id)
    }
    getContent() {
      return this.el.innerHTML
    }
    setContent(content) {
      this.el.innerHTML = content
    }
    hasClass(className) {
      return !!Array.from(this.el.classList).find((item) => item === className)
    }
    removeClass(className) {
      //el.classList返回的是类数组对象
      this.el.classList.remove(className)
    }
    addClass(className) {
      this.el.classList.add(className)
      return this //支持链式
    }
    getStyle(...attr) {
      const style = window.getComputedStyle(this.el)
      return attr.map((item) => style[item])
    }
    setStyle(obj) {
      for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
          this.el.style[key] = obj[key]
        }
      }
      return this //支持链式
    }
  }
  const handler = new Handler('app')
</script>
